package com.quan.upms.rpc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Condition;

import com.quan.common.base.exception.BizException;
import com.quan.common.core.support.BaseService;
import com.quan.upms.rpc.api.UpmsRoleService;
import com.quan.upms.rpc.dao.mapper.UpmsRolePermissionAssignMapper;
import com.quan.upms.rpc.dao.mapper.UpmsUserRoleAssignMapper;
import com.quan.upms.rpc.extend.UpmsRoleDto;
import com.quan.upms.rpc.model.UpmsRole;
import com.quan.upms.rpc.model.UpmsRolePermissionAssign;
import com.quan.upms.rpc.model.UpmsUserRoleAssign;

@Service
public class UpmsRoleServiceImpl extends BaseService<UpmsRole> implements UpmsRoleService {
	
	@Autowired
	private UpmsRolePermissionAssignMapper assignMapper ;
	
	@Autowired
	private UpmsUserRoleAssignMapper userRoleAssignMapper ;
	
	@Autowired
	private UpmsRolePermissionAssignMapper upmsRolePermissionAssignMapper ;
	
	@Override
	public int deleteById(Object id) {
		//删除该角色下的权限关联关系
		Condition condition = new Condition(UpmsRolePermissionAssign.class) ;
		condition.createCriteria().andEqualTo("roleId", id);
		this.upmsRolePermissionAssignMapper.deleteByCondition(condition) ;
		
		//删除该角色下的用户关联关系
		Condition ruc = new Condition(UpmsUserRoleAssign.class) ;
		ruc.createCriteria().andEqualTo("roleId", id);
		this.userRoleAssignMapper.deleteByCondition(ruc) ;
		
		return super.deleteById(Integer.valueOf(id.toString()));
	}
	
	@Override
	public int deleteByIds(String ids) throws BizException {
		String[] split = ids.split(",");
		for (String id : split) {
			//删除该角色下的权限关联关系
			Condition condition = new Condition(UpmsRolePermissionAssign.class) ;
			condition.createCriteria().andEqualTo("roleId", id);
			this.upmsRolePermissionAssignMapper.deleteByCondition(condition) ;
			
			//删除该角色下的用户关联关系
			Condition ruc = new Condition(UpmsUserRoleAssign.class) ;
			ruc.createCriteria().andEqualTo("roleId", id);
			this.userRoleAssignMapper.deleteByCondition(ruc) ;
		}
		return super.deleteByIds(ids);
	}

	@Override
	public void addPermissionToRole(UpmsRoleDto dto) {
		//把已有的角色权限先删除
		UpmsRolePermissionAssign delByRoleId = new UpmsRolePermissionAssign() ;
		delByRoleId.setRoleId(dto.getId());
		this.assignMapper.delete(delByRoleId) ;
		
		if(null != dto.getPermissionIds() && !dto.getPermissionIds().isEmpty()) {
			List<UpmsRolePermissionAssign> recordList = new ArrayList<UpmsRolePermissionAssign>() ;
			List<Integer> permissionIds = dto.getPermissionIds() ;
			for (Integer permissionId : permissionIds) {
				UpmsRolePermissionAssign entity = new UpmsRolePermissionAssign() ;
				entity.setRoleId(dto.getId());
				entity.setPermissionId(permissionId);
				recordList.add(entity) ;
			}
			
			//批量插入
			this.assignMapper.insertList(recordList);
		}
	}

	@Override
	public List<UpmsRolePermissionAssign> getRolePermissionAssign(Integer roleId) {
		Condition condition = new Condition(UpmsRolePermissionAssign.class) ;
		condition.createCriteria().andEqualTo("roleId", roleId);
		return this.assignMapper.selectByCondition(condition) ;
	}

	
	@Override
	public void removeRoleUsers(String roleAssingIds) {
		this.userRoleAssignMapper.deleteByIds(roleAssingIds) ;
	}
}
